import helpers
from unittest import TestCase
from pagamigo.RequestSession import RequestSession
from pagamigo.Authentication import flow as authentication_flow
from pagamigo.PeerToPeer import flow as peer_to_peer_flow


def logger(data):
    print(data)

# This test fails sometimes with a 400 bad request exception. It passes sometimes.
# I'm not sure why this happens but I found that running the test multiple times in a row
# will lead at least 1 pass.

class TestPeerToPeer(TestCase):
    def test_peer_to_peer_flow(self):
        config = helpers.get_paypal_sandbox_config_from_json()
        session = RequestSession("https://www.sandbox.paypal.com", logger)

        # Log the user in and then send funds
        response = authentication_flow(session, config["paypal_sandbox_email"],
                                       config["paypal_sandbox_password"],
                                       config["paypal_sandbox_country"])

        # an exception will be thrown if this operation doesn't succeed
        is_success = peer_to_peer_flow(session, config["paypal_sandbox_recipient_email"],
                                     config["test_amount_for_peer_to_peer"])
        assert is_success is True
